package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeeTrainingMapper;
import com.ruoyi.business.domain.EmployeeTraining;
import com.ruoyi.business.service.IEmployeeTrainingService;

import javax.validation.Validator;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 员工培训记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class EmployeeTrainingServiceImpl  extends BaseServiceImpl implements IEmployeeTrainingService
{
    @Autowired
    private EmployeeTrainingMapper employeeTrainingMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询员工培训记录
     * 
     * @param id 员工培训记录主键
     * @return 员工培训记录
     */
    @Override
    public EmployeeTraining selectEmployeeTrainingById(Long id)
    {
        return employeeTrainingMapper.selectEmployeeTrainingById(id);
    }

    /**
     * 查询员工培训记录列表
     * 
     * @param employeeTraining 员工培训记录
     * @return 员工培训记录
     */
    @Override
    public List<EmployeeTraining> selectEmployeeTrainingList(EmployeeTraining employeeTraining)
    {
        return employeeTrainingMapper.selectEmployeeTrainingList(employeeTraining);
    }

    /**
     * 新增员工培训记录
     * 
     * @param employeeTraining 员工培训记录
     * @return 结果
     */
    @Override
    public int insertEmployeeTraining(EmployeeTraining employeeTraining)
    {
        employeeTraining.setCreateTime(DateUtils.getNowDate());
        return employeeTrainingMapper.insertEmployeeTraining(employeeTraining);
    }

    /**
     * 修改员工培训记录
     * 
     * @param employeeTraining 员工培训记录
     * @return 结果
     */
    @Override
    public int updateEmployeeTraining(EmployeeTraining employeeTraining)
    {
        employeeTraining.setUpdateTime(DateUtils.getNowDate());
        return employeeTrainingMapper.updateEmployeeTraining(employeeTraining);
    }

    /**
     * 批量删除员工培训记录
     * 
     * @param ids 需要删除的员工培训记录主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeTrainingByIds(Long[] ids)
    {
        return employeeTrainingMapper.deleteEmployeeTrainingByIds(ids);
    }

    /**
     * 删除员工培训记录信息
     * 
     * @param id 员工培训记录主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeTrainingById(Long id)
    {
        return employeeTrainingMapper.deleteEmployeeTrainingById(id);
    }

    @Override
    public String importData(List<EmployeeTraining> trainingList, boolean updateSupport, String operName) {
    if (trainingList == null || trainingList.isEmpty()) {
        throw new ServiceException("导入员工培训数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();
    int i = 1;
    for (EmployeeTraining training : trainingList) {
        try {
            i++;
            if (training.getAwardYear() == null || training.getAwardYear().equals("") || training.getEmployeeId() ==null || training.getEmployeeId().equals("") || !isAwardYearValid(training.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个员工培训
            EmployeeTraining existingTraining = employeeTrainingMapper.selectEmployeeTrainingById(training.getId());
            if (existingTraining == null) {
                // 验证数据
                BeanValidators.validateWithException(validator, training);
                // 设置初始值
                training.setCreateTime(DateUtils.getNowDate());
                training.setCreateBy(operName);
                employeeTrainingMapper.insertEmployeeTraining(training);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工培训 "  + " 导入成功");
            } else if (updateSupport) {
                // 验证数据
                BeanValidators.validateWithException(validator, training);
                training.setId(existingTraining.getId());
                training.setUpdateTime(DateUtils.getNowDate());
                training.setUpdateBy(operName);
                employeeTrainingMapper.updateEmployeeTraining(training);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、员工培训 "  + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、员工培训 " +  " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、员工培训 "  + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }
        setCacheInfo("员工培训",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
